/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: btserver.h.
*/

#ifndef BTSERVER_H
#define BTSERVER_H

#include <qmobilityglobal.h>

#include <QtCore/QObject>

QTM_BEGIN_NAMESPACE
class QRfcommServer;
class QBluetoothSocket;
class QBluetoothAddress;
QTM_END_NAMESPACE

QTM_USE_NAMESPACE

//! Bluetooth server class.
/*!
  This class creates an RFCOMM server, which works as a server in a Bluetooth
  connection between two devices. The server is listening for new connections to
  the device's Bluetooth address and selected port.
*/
class BtServer : public QObject
{
    Q_OBJECT

public:
    //! Constructor.
    explicit BtServer(QObject *parent = 0);

    //! Destructor.
    virtual ~BtServer();

    //! Creates the RFCOMM server.
    /*!
      \param address Local address on which the server starts to listen for new connections.
      \param port The port on which the server starts to listen.
     */
    void startServer(const QBluetoothAddress &address, quint16 port);

    //! Disconnects the client from the Bluetooth service.
    void closeClient();

    //! Sends data to remote device.
    /*!
      \param sharedData A byte array which contains the data to be sent.
     */
    void writeSocket(const QByteArray &sharedData);

private Q_SLOTS:
    //! New client connected to the Bluetooth service.
    void clientConnected();

    //! Client disconnected from the Bluetooth service.
    void clientDisconnected();

    //! Reads the received data.
    void readSocket();

Q_SIGNALS:
    //! Signalled when a new client is connected.
    /*!
      \param clientName The name of the remote device.
     */
    void newClient(const QString &clientName);

    //! Signalled when data is received from a remote device.
    /*!
     \param message A byte array which contains the received data.
     */
    void messageReceived( const QByteArray &message);

    //! Signalled when a client is disconnected from the Bluetooth service.
    /*!
      \param clientName The name of the client which was disconnected.
     */
    void clientDisconnected(const QString &clientName);

private:
    QRfcommServer *m_rfcommServer; //!< RFCOMM server.

    QBluetoothSocket *m_clientSocket; //!< Bluetooth socket.
};


#endif // BTSERVER_H
